package org.xteam.engine.geom;

public class Matrix {

	public static final Matrix identity = new Matrix(new double[][] {
			{1.0, 0.0, 0.0, 0.0},
			{0.0, 1.0, 0.0, 0.0},
			{0.0, 0.0, 1.0, 0.0},
			{0.0, 0.0, 0.0, 1.0},
	});

	private double[][] values;
	
	public Matrix(double[][] values) {
		this.values = values;
	}

	public Point3D mutiply(Point3D p) {
		return new Point3D(
				p.x * values[0][0] + p.y * values[0][1] + p.z * values[0][2] + p.m * values[0][3],
				p.x * values[1][0] + p.y * values[1][1] + p.z * values[1][2] + p.m * values[1][3],
				p.x * values[2][0] + p.y * values[2][1] + p.z * values[2][2] + p.m * values[2][3],
				p.x * values[3][0] + p.y * values[3][1] + p.z * values[3][2] + p.m * values[3][3]);
	}
	
	public Matrix mutiply(Matrix matrix) {
		Matrix m = new Matrix(new double[values.length][values[0].length]);
		for (int r = 0; r < values.length; ++r) {
			for (int c = 0; c < values[0].length; ++c) {
				double s = 0.0;
				for (int k = 0; k < values.length; ++k) {
					s += values[r][k] * matrix.values[k][c];
				}
				m.values[r][c] = s;
			}
		}
		return m;
	}

	public static Matrix rotateX(double angle) {
		double theta = Math.toRadians(angle);
		return new Matrix(new double[][] {
				{1.0, 0.0, 0.0, 0.0},
				{0.0, Math.cos(theta), -Math.sin(theta), 0.0},
				{0.0, Math.sin(theta), Math.cos(theta), 0.0},
				{0.0, 0.0, 0.0, 1.0}
		});
	}

	public static Matrix rotateY(int angle) {
		double theta = Math.toRadians(angle);
		return new Matrix(new double[][] {
				{Math.cos(theta), 0.0, Math.sin(theta), 0.0},
				{0.0, 1.0, 0.0, 0.0},
				{-Math.sin(theta), 0.0, Math.cos(theta), 0.0},
				{0.0, 0.0, 0.0, 1.0}
		});
	}

	public static Matrix rotateZ(int angle) {
		double theta = Math.toRadians(angle);
		return new Matrix(new double[][] {
				{Math.cos(theta), -Math.sin(theta), 0.0, 0.0},
				{Math.sin(theta), Math.cos(theta), 0.0, 0.0},
				{0.0, 0.0, 1.0, 0.0},
				{0.0, 0.0, 0.0, 1.0}
		});
	}

	
}
